package com.lanchetech.user.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.CustomPageDataVO;
import com.lanchetech.bean.vo.CustomPageEditVO;
import com.lanchetech.common.enums.CustomPageDataTypeEnum;
import com.lanchetech.dao.*;
import com.lanchetech.entity.*;
import com.lanchetech.service.DaoService;
import com.lanchetech.user.service.WebService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WebServiceImpl implements WebService {

    @Autowired
    WebConfigMapper webConfigMapper;

    @Autowired
    CustomPageMapper customPageMapper;

    @Autowired
    CustomPageDataMapper customPageDataMapper;

    @Autowired
    DaoService daoService;

    @Autowired
    PosterMapper posterMapper;

    @Autowired
    OnePostMapper onePostMapper;

    @Autowired
    HipoConfigMapper hipoConfigMapper;

    @Override
    public ResultData<List<WebConfig>> getWebConfigList() {
        List<WebConfig> list = webConfigMapper.findAll();
        return new ResultData<>(list);
    }


    @Override
    public ResultData<CustomPageEditVO> getCustomPageDetail(Long id) {
        CustomPageEditVO vo = new CustomPageEditVO();

        vo.setCustomPage(customPageMapper.selectByPrimaryKey(id));
        List<CustomPageData> customPageDataList = customPageDataMapper.findAllByCustomPageId(id);

        // 获取所有的商品,优惠券信息
        // update 如果是添加到购物、获取优惠券，则不用去获取商品信息和优惠券信息
        List<Long> spuIds = new ArrayList<>();
        List<Long> couponIds = new ArrayList<>();
        customPageDataList.stream().forEach(item -> {
            if (CustomPageDataTypeEnum.PRODUCT.getType().equals(item.getType())) {
                spuIds.add(item.getRelateId());
            }
            if (CustomPageDataTypeEnum.COUPON.getType().equals(item.getType())) {
                couponIds.add(item.getRelateId());
            }
        });

        Map<Long, Spu> spuMap = CollectionUtils.isEmpty(spuIds) ? new HashMap<>() : daoService.getSpuMap(spuIds);
        Map<Long, Coupon> couponMap = CollectionUtils.isEmpty(couponIds) ? new HashMap<>() : daoService.getCouponMap(couponIds);

        List<CustomPageDataVO> customPageDataVOList = new ArrayList<>();
        customPageDataList.stream().forEach(item -> {
            CustomPageDataVO customPageDataVO = new CustomPageDataVO();
            BeanUtils.copyProperties(item, customPageDataVO);
            if (CustomPageDataTypeEnum.PRODUCT.getType().equals(item.getType())) {
                customPageDataVO.setSpu(spuMap.get(item.getRelateId()));
            }
            if (CustomPageDataTypeEnum.COUPON.getType().equals(item.getType())) {
                customPageDataVO.setCoupon(couponMap.get(item.getRelateId()));
            }
            customPageDataVOList.add(customPageDataVO);
        });

        vo.setCustomPageDataList(customPageDataVOList);

        return new ResultData<>(vo);
    }

    @Override
    public ResultData<BasePageResp<Poster>> getPosterPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<Poster> list = posterMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<String> getWebConfigByName(String name) {
        return new ResultData<>(webConfigMapper.findValueByName(name));
    }


    @Override
    public ResultData<BasePageResp<OnePost>> getOnePostPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<OnePost> list = onePostMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<List<HipoConfig>> getHipoConfigList() {
        return new ResultData<>(hipoConfigMapper.findAll());
    }
}